import 'package:courseapp/entity/coursedto.dart';
import 'package:courseapp/entity/stusignsumdto.dart';
import 'package:courseapp/http/HttpUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AllCourseWidget extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return AllCourseState();
  }

}

class AllCourseState extends State<AllCourseWidget> {


  List<String> week = ["周一","周二","周三","周四","周五"];
  Coursedto coursedto = null;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    HttpUtil.getInstance().postAllStuCourse().asStream().listen((course){
      setState(() {
        this.coursedto = course;
      });
    });
  }

  @override
  Widget build(BuildContext context) {

    if(coursedto == null) {
      // TODO: implement build
      return Scaffold(
        body: Container(alignment:Alignment.center,child:Text("今日课程")),
      );
    } else {
      return Scaffold(
          backgroundColor: Colors.white,
          body: ListView.builder(itemCount: coursedto.data.length,
            itemBuilder: (context,index){
              String courseName =  coursedto.data[index].courseName;
              int weekIndex = coursedto.data[index].whatDay - 2;
              String weekDay =  week[weekIndex];
              return FlatButton(
                  padding: EdgeInsets.all(0),
                  onPressed: (){
                    int courseId =  coursedto.data[index].id;
                    showSumCourse(courseId);
                  },
                  child:DecoratedBox(decoration: BoxDecoration( color: Colors.white,
                  border: Border(
                    bottom: Divider.createBorderSide(context, color: Colors.grey),
                  )),
                  child:Container(padding: EdgeInsets.all(5) ,child:Column(children: <Widget>[
                        Container(alignment: Alignment.centerLeft, child:Text("课程:$courseName")),
                        Container(alignment: Alignment.centerLeft, child:Text("时间:$weekDay")),],)
                  )
              ));

            },
            itemExtent: 50,
          )
      );
    }
  }

  Future<void> showSumCourse(int courseId) async {
    Stusignsumdto stusignsumdto = await HttpUtil.getInstance().queryStuSum(courseId);
    int signNum = stusignsumdto.data.signNum;
    int notSignNum = stusignsumdto.data.notSignNum;
    int totalNum = stusignsumdto.data.totalNum;
    showDialog(context: context,child: Container(
      alignment: Alignment.center,
      child:
      SizedBox(
      width: 200,
      height: 200,
      child: Column(
        children: <Widget>[
          Container(alignment: Alignment.centerLeft,padding: EdgeInsets.all(2),child:Text("签到: $signNum",style: TextStyle(color: Colors.white,fontSize: 20 ,decoration: TextDecoration.none))),
          Container(alignment: Alignment.centerLeft,padding: EdgeInsets.all(2),child:Text("未签到: $notSignNum",style: TextStyle(color: Colors.white,fontSize: 20 ,decoration: TextDecoration.none))),
          Container(alignment: Alignment.centerLeft,padding: EdgeInsets.all(2),child:Text("总签到: $totalNum",style: TextStyle(color: Colors.white,fontSize: 20 ,decoration: TextDecoration.none))),
        ],
      ),
      )
    ));

  }



}